﻿using System.Configuration;
using System.Linq;

namespace ExcelToVT
{
    public class Ilac
    {
        public int id { get; set; }
        public string skrs_ilac_adi { get; set; }
        public decimal skrs_barkod { get; set; }
        public string skrs_firma { get; set; }
        public string skrs_recete_turu { get; set; }
        public string skrs_durum { get; set; }
        public string Aciklama { get; set; }

        public Ilac() { }

        public override bool Equals(object _iegm)
        {
            var iegm = _iegm as IEGMIlac;
            if (iegm == null)
            {
                return false;
            }

            bool bDegisti = iegm.skrs_ilac_adi.Equals(this.skrs_ilac_adi) &&
                            iegm.skrs_durum.Equals(this.skrs_durum) &&
                            iegm.skrs_firma.Equals(this.skrs_firma) &&
                            iegm.skrs_recete_turu.Equals(this.skrs_recete_turu) &&
                            iegm.Aciklama.Equals(this.Aciklama);
            if (!bDegisti)
            {
                this.skrs_ilac_adi = iegm.skrs_ilac_adi;
                this.skrs_durum = iegm.skrs_durum;
                this.skrs_firma = iegm.skrs_firma;
                this.skrs_recete_turu = iegm.skrs_recete_turu;
                this.Aciklama = iegm.Aciklama;
            }
            return !bDegisti;
        }

        public Ilac(string _sCnnStr, IEGMIlac _iegm)
        {
            Kontext ktx = new Kontext(ConfigurationManager.ConnectionStrings[_sCnnStr].ConnectionString);
            Ilac ilac = ktx.Ilaclar.FirstOrDefault(k => k.skrs_barkod.Equals(_iegm.skrs_barkod));
            if (ilac == null)
            {
                this.skrs_barkod = _iegm.skrs_barkod;
                this.skrs_durum = _iegm.skrs_durum;
                this.skrs_firma = _iegm.skrs_firma;
                this.skrs_ilac_adi = _iegm.skrs_ilac_adi;
                this.skrs_recete_turu = _iegm.skrs_recete_turu;
                this.Aciklama = _iegm.Aciklama;
                ktx.Ilaclar.Add(this);
                int a = ktx.SaveChanges();
            }
            else
            {
                if (ilac.Equals(_iegm))
                {
                    ktx.SaveChanges();
                }
            }
        }
    }
}